package com.sgb.kjwl.view.widget.transport

import android.annotation.SuppressLint
import android.app.Activity
import android.content.Context
import android.util.Log
import android.view.LayoutInflater
import android.view.MotionEvent
import android.view.View
import android.view.ViewGroup
import androidx.databinding.DataBindingUtil
import com.bumptech.glide.Glide
import com.sgb.kjwl.R
import com.sgb.kjwl.databinding.LayoutLogisticsWorkbenchBinding
import com.swgk.core.util.PermissionCheckUtil
import com.sgb.kjwl.utils.TouchHelper
import com.swgk.core.util.UserOperating
import com.swgk.core.mmkvutil.MkvUtils

/**
 * @author: 杨畔
 * @date: 4/2/21 11:26 AM
 * @Description: 物流工作台
 */
class LogisticsWorkbench : TouchHelper.TouchListenerProxy(), LogisticsWorkbenchInterface,
    View.OnClickListener, View.OnTouchListener {
    //手势提示动画
    private val TIP_ANIM = "TIP_ANIM"

    //
    private var mBinding: LayoutLogisticsWorkbenchBinding? = null
    private var mWorkbenchView: View? = null

    //手势帮助类
    private var mTouchHelper: TouchHelper? = null

    companion object {
        @JvmStatic
        fun newInstance() = LogisticsWorkbench()
    }

    @SuppressLint("InflateParams")
    override fun attach(context: Context) {
        if (context !is Activity) {
            throw IllegalArgumentException("参数错误，只能是Activity上下文")
        }
        //依附工作台控件到页面
        mBinding = DataBindingUtil.inflate(
            LayoutInflater.from(context),
            R.layout.layout_logistics_workbench,
            null,
            false
        )
        context.window.addContentView(
            mBinding?.root,
            ViewGroup.LayoutParams(
                ViewGroup.LayoutParams.MATCH_PARENT,
                ViewGroup.LayoutParams.MATCH_PARENT
            )
        )
        //判断身份展示工作台
        checkUser(context)
        //添加监听
        initEvent()
    }

    /**
     * 检查用户身份
     */
    fun checkUser(context: Context) {
        //重置工作台
        mWorkbenchView = null
        //防止重复添加
        mBinding?.flWorkbenchRoot?.removeAllViews()
        //个人用户，已实名认证且未司机认证，显示货主工作台，不显示车主工作台；已司机认证，显示车主工作台，不显示货主工作台。
        if (UserOperating.getInstance().isPersonal) {
            if (UserOperating.getInstance().isAuthRealName && !UserOperating.getInstance().isAuthDriver) {
                ownerMode(context)
            }
            if (UserOperating.getInstance().isAuthDriver) {
                driverMode(context)
            }
        }
        //团队用户，团队经营类型为物流信息中介，显示货主工作台
        if (UserOperating.getInstance().isTeam && UserOperating.getInstance().isAuthAgentComputer) {
            ownerMode(context)
        }
        //企业用户，有货主功能操作权限，显示货主工作台
        if (UserOperating.getInstance().isCompany && UserOperating.getInstance()
                .hasCompanyPermission(
                    PermissionCheckUtil.KEY_HUOYUANGUANLI
                )
        ) {
            ownerMode(context)
        }
        //添加页面工作台
        if (mWorkbenchView == null) {
            mBinding?.cbWorkbenchBtn?.visibility = View.GONE
        } else {
            mBinding?.cbWorkbenchBtn?.visibility = View.VISIBLE
            mBinding?.flWorkbenchRoot?.addView(mWorkbenchView)
        }
    }

    @SuppressLint("ClickableViewAccessibility")
    private fun initEvent() {
        mBinding?.onClick = this
        mBinding?.flWorkbenchRoot?.setOnTouchListener(this)
        mBinding?.cbWorkbenchBtn?.setOnCheckedChangeListener { _, isChecked ->
            if (isChecked) {
                mBinding?.flWorkbenchRoot?.apply {
                    removeAllViews()
                    mWorkbenchView?.let { addView(it) }
                }
                //第一次展示提示动画
                val isShowTip = MkvUtils.get().mmkv.decodeBool(TIP_ANIM, true)
                takeIf { isShowTip }?.let {
                    mBinding?.ivWorkbenchGif
                }?.apply {
                    MkvUtils.get().mmkv.encode(TIP_ANIM, false)
                    mBinding?.llWorkbenchTip?.visibility = View.VISIBLE
                    Glide.with(context).asGif().load(R.drawable.gif_scroll_right).into(this)
                }
                mBinding?.groupWorkbench?.visibility = View.VISIBLE
            } else {
                mBinding?.groupWorkbench?.visibility = View.GONE
                mBinding?.llWorkbenchTip?.visibility = View.GONE
            }
        }
    }

    /**
     * 司机模式
     */
    private fun driverMode(context: Context) {
        mWorkbenchView = LogisticsDriverWorkbenchView(context)
        mBinding?.cbWorkbenchBtn?.setButtonDrawable(R.drawable.selector_workbench_driver_btn)
    }

    /**
     * 货主模式
     */
    private fun ownerMode(context: Context) {
        mWorkbenchView = LogisticsOwnerWorkbenchView(context)
        mBinding?.cbWorkbenchBtn?.setButtonDrawable(R.drawable.selector_workbench_owner_btn)
    }

    override fun showWorkbench() {
        mBinding?.root?.visibility = View.VISIBLE
    }

    override fun hideWorkbench() {
        mBinding?.root?.visibility = View.GONE
    }

    override fun showWorkbenchDialog() {
        mBinding?.cbWorkbenchBtn?.apply { isChecked = !isChecked }
        mBinding?.groupWorkbench?.visibility = View.VISIBLE
//        mBinding?.mlWorkbench?.transitionToEnd()
    }

    override fun dismissWorkbenchDialog() {
        mBinding?.cbWorkbenchBtn?.apply { isChecked = !isChecked }
        mBinding?.groupWorkbench?.visibility = View.GONE
//        mBinding?.mlWorkbench?.transitionToStart()
    }

    override fun onClick(v: View?) {
        when (v) {
            mBinding?.vWorkbenchBk -> {
                dismissWorkbenchDialog()
            }
        }
    }

    @SuppressLint("ClickableViewAccessibility")
    override fun onTouch(v: View?, event: MotionEvent?): Boolean {
        //手势滑动隐藏工作台
        if (mTouchHelper == null) {
            mTouchHelper = TouchHelper.newInstance().addTouchListener(this)
        }
        mTouchHelper?.onTouch(v, event)
        return true
    }

    /**
     * 右滑手势
     */
    override fun rightScroll() {
        Log.e("workbench", "rightScroll")
        dismissWorkbenchDialog()
    }
}